package com.meng.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class WebSocketServer {

	public static void main(String[] args) throws Exception {
		ServerSocket server = new ServerSocket(8888);

		while (true) {
			final Socket client = server.accept();

			new Thread() {
				public void run() {
					try {
						PrintWriter pw;
						pw = new PrintWriter(client.getOutputStream());
						pw.print("Hello, wellcome to server!");

						BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));

						for (;;) {
							String msg = br.readLine();
							System.out.println(client.getRemoteSocketAddress() + "发来消息：" + msg);
							pw.print("server received your msg: " + msg);
						}

					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}.start();
		}

	}
}
